package com.song.email.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.song.workpool.control.SendEmailTaskControl;

public class EmailCenter {
	private Log log = LogFactory.getLog(EmailCenter.class);
	private static EmailCenter emailCenter = null;
	private boolean isLoadEmailInfo = false;
	
	private EmailCenter(){}
	
	public synchronized static EmailCenter getInstance(){
		if(null == emailCenter){
			emailCenter = new EmailCenter();
		}
		return emailCenter;
	}

	public boolean loadEmailInfo(){
		log.info("正在加载文件数据...");
		try {
			AuthCenter.getInstance().init();
			log.info("加载auths.xml成功.auth数:" +AuthCenter.getInstance().getAuthListSize());
			MailtoCenter.getInstance().init();
			log.info("加载mailtoList.txt成功.收件人个数:" + MailtoCenter.getInstance().getMailtoListSize());
			WorkCenter.getInstance().init();
			log.info("加载works.xml成功.任务数:" + WorkCenter.getInstance().getWorkListSize());
			log.info("加载文件数据成功...");
			isLoadEmailInfo = true;
		}catch (Exception e) {
			log.error("加载文件数据失败...",e);
		}
		return isLoadEmailInfo;
	}
	
	public void doWork(){
		if(isLoadEmailInfo || loadEmailInfo()){
			SendEmailTaskControl.getInstance().SendEmailAsy();
			log.info("发送任务到工作线程成功");
		}else{
			log.error("发送任务到工作线程失败.因为加载文件数据失败.");
		}
		
	}
}
